﻿using ProjBackEnd.Areas.Statistics.Models;
using ProjBackEnd.DBInstance;
using ProjBackEnd.Lib;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Web;

namespace ProjBackEnd.DAL
{
    public class statisticsLogic
    {

        public static RespStProj GetStProjPaging(string _strMemberid, string _strComcd, int _iPageSize, int _iCurrPage, int _iProjid, int _iCusid, DateTime? _dtSdt, DateTime? _dtEdt, string[] _strsPid)
        {
            RespStProj respReturn = new RespStProj();
            bool hasNextPage = false;
            StatisticsDataContext stctx = new StatisticsDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                if (_iPageSize == 0) throw new Exception("每页显示条数不能为0");
                int iRecTotal, iPageCount;
                iRecTotal = stctx.VIEW_ST_PROJINFO.Count(n => (_iProjid == 0 ? true : n.projid == _iProjid)
                                                                            && (string.IsNullOrEmpty(_strComcd) ? true : n.comcd == _strComcd)
                                                                            && (_iCusid == 0 ? true : n.cusid == _iCusid)
                                                                            && (_dtSdt == null ? true : n.sdt > _dtSdt)
                                                                            && (_dtEdt == null ? true : n.sdt < _dtEdt) //仅比较开始时间
                                                                            && (_strsPid.Length == 0 ? true : _strsPid.Contains(n.pid))
                                                                            && ((n.purmemberid == _strMemberid) || n.createid == _strMemberid));
                if ((iRecTotal / _iPageSize - _iCurrPage) > 0) hasNextPage = true;
                else if ((iRecTotal / _iPageSize - _iCurrPage) == 0)
                {
                    if ((iRecTotal % _iPageSize) > 0) hasNextPage = true;
                }
                iPageCount = (iRecTotal == 0 ? 0 : iRecTotal / _iPageSize + 1);
                List<ProjInfoExtSt> lstProjExt = stctx.VIEW_ST_PROJINFO.Where(n => (_iProjid == 0 ? true : n.projid == _iProjid)
                                                                                                && (string.IsNullOrEmpty(_strComcd) ? true : n.comcd == _strComcd)
                                                                                                && (_iCusid == 0 ? true : n.cusid == _iCusid)
                                                                                                && (_dtSdt == null ? true : n.sdt > _dtSdt)
                                                                                                && (_dtEdt == null ? true : n.sdt < _dtEdt)
                                                                                                && (_strsPid.Length == 0 ? true : _strsPid.Contains(n.pid))
                                                                                                && ((n.purmemberid == _strMemberid) || n.createid == _strMemberid))
                                                                                .OrderBy(n => n.projid)
                                                                                .Take(_iPageSize * _iCurrPage).Skip(_iPageSize * (_iCurrPage - 1)).AsEnumerable()
                                                                                .Select(n => new ProjInfoExtSt
                                                                                {
                                                                                     amt_paytotal=n.amt_paytotal, amt_rectotal=n.amt_rectotal, 
                                                                                    comcd = n.comcd,
                                                                                    createdt = n.createdt.ToString("yyyy-MM-dd"),
                                                                                    createid = n.createid,
                                                                                    creator = n.creator,
                                                                                    ctno = n.ctno,
                                                                                    cusid = n.cusid,
                                                                                    cusnm = n.cusnm,
                                                                                    deftaxrate = n.deftaxrate,
                                                                                    edt = (n.edt == null ? "" : ((DateTime)n.edt).ToString("yyyy-MM-dd")),
                                                                                    findt = (n.findt == null ? "" : ((DateTime)n.findt).ToString("yyyy-MM-dd")),
                                                                                    optlb = GlobalSettings.ENTITYOPERATION_NONE,
                                                                                    pid = n.pid,
                                                                                    pidnm = n.pidnm,
                                                                                    pprojid = n.pprojid,
                                                                                    projdesc = n.projdesc,
                                                                                    projid = n.projid,
                                                                                    projnm = n.projnm,
                                                                                    remark = n.remark,
                                                                                    sdt = (n.sdt == null ? "" : n.sdt.ToString("yyyy-MM-dd"))
                                                                                }).ToList();
                respReturn.result = new ResultStProj { hasnextpage = hasNextPage, pagecount = iPageCount,  projstlist = lstProjExt, reccount = iRecTotal };
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (stctx.Connection.State != ConnectionState.Closed) stctx.Connection.Close(); }
            return respReturn;
        }

        public static RespStBizFee GetStBizFeeList(string _strMemberid, string _strComcd, int _iProjid, int _iProjdtlid, int _iBillid, string _strPid, string _strRplb, int _iCusid, int _iCtid, int _iCtSubid, string _strCreateid)
        {
            RespStBizFee respReturn = new RespStBizFee();
            StatisticsDataContext stctx = new StatisticsDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                List<BizFeeExtInfoSt> lst = stctx.VIEW_ST_FEEDETAILINFO.Where(n => (_iProjid == 0 ? true : n.projid == _iProjid)
                                                                                                                       && (string.IsNullOrEmpty(_strRplb) ? true : n.rplb == _strRplb)
                                                                                                                       && (string.IsNullOrEmpty(_strCreateid) ? true : n.createid == _strCreateid)
                                                                                                                       && (_iProjdtlid == 0 ? true : (_iProjdtlid == -1 ? n.projdtlid == 0 : n.projdtlid == _iProjdtlid))
                                                                                                                       && (_iBillid == 0 ? true : n.billid == _iBillid)
                                                                                                                       && (_iCusid == 0 ? true : n.cusid == _iCusid)
                                                                                                                       && (_iCtid == 0 ? true : n.ctid == _iCtid)
                                                                                                                       && (_iCtSubid == 0 ? true : n.ctsubid == _iCtSubid)
                                                                                                                       && (n.comcd == _strComcd)
                                                                                                                       && (string.IsNullOrEmpty(_strPid) ? true : n.pid == _strPid)).AsEnumerable()
                                                                                                        .Select(n => new BizFeeExtInfoSt
                                                                                                        {
                                                                                                            amt_payprice = n.amt_payprice,
                                                                                                            amt_paytotal = n.amt_paytotal,
                                                                                                            amt_recprice = n.amt_recprice,
                                                                                                            amt_rectotal = n.amt_rectotal,
                                                                                                            billid = n.billid,
                                                                                                            billpid = n.billpid,
                                                                                                            billpidnm = n.billpidnm,
                                                                                                            brand = n.brand,
                                                                                                            brandnm = n.brandnm,
                                                                                                            comcd = n.comcd,
                                                                                                            createdt = n.createdt.ToString("yyyy-MM-dd"),
                                                                                                            creator = n.creator,
                                                                                                            ctid = n.ctid,
                                                                                                            ctsubid = n.ctsubid,
                                                                                                            cuscd = n.cuscd,
                                                                                                            cusid = n.cusid,
                                                                                                            cusnm = n.cusnm,
                                                                                                            feeid = n.feeid,
                                                                                                            invotp = n.invotp,
                                                                                                            invotpnm = n.invotpnm,
                                                                                                            model = n.model,
                                                                                                            num = n.num,
                                                                                                            objdesc = n.objdesc,
                                                                                                            objtp = n.objtp,
                                                                                                            objtpnm = n.objtpnm,
                                                                                                            optlb = GlobalSettings.ENTITYOPERATION_NONE,
                                                                                                            pid = n.pid,
                                                                                                            piddesc = n.piddesc,
                                                                                                            projdtlid = n.projdtlid,
                                                                                                            projid = n.projid,
                                                                                                            projdtlnm = n.dtlnm,
                                                                                                            amt_bill = (n.rplb == GlobalSettings.FEELB_PAY ? n.amt_pay : n.amt_rec),
                                                                                                            amt_settled = Math.Round(n.rplb == GlobalSettings.FEELB_PAY ? (n.amt_paytotal * (n.amt_pay == 0 ? 0 : (n.amt_settled / n.amt_pay))) : (n.amt_rectotal * (n.amt_rec == 0 ? 0 : (n.amt_settled / n.amt_rec))), 2),
                                                                                                            amt_settled_total = n.amt_settled,
                                                                                                            settlepercent = Math.Round(n.rplb == GlobalSettings.FEELB_PAY ? (n.amt_pay == 0 ? 1 : (n.amt_settled / n.amt_pay)) : (n.amt_rec == 0 ? 1 : (n.amt_settled / n.amt_rec)), 2),
                                                                                                            purchasdt = n.purchasdt.ToString("yyyy-MM-dd HH:mm"),
                                                                                                            remark = n.remark,
                                                                                                            spec = n.spec,
                                                                                                            taxrate = n.taxrate,
                                                                                                            rplb = n.rplb,
                                                                                                            unit = n.unit
                                                                                                        }).ToList();
                respReturn.result = new ResultStBizFee { feestlist = lst };
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (stctx.Connection.State != ConnectionState.Closed) stctx.Connection.Close(); }
            return respReturn;
        }

        public static RespStBizFee GetStBizArrears(string _strMemberid,string _strComcd,int _iProjid, string _strRplb, int _iCusid,string _strArrearslb)
        {
            RespStBizFee respReturn = new RespStBizFee();
            StatisticsDataContext stctx = new StatisticsDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                List<BizFeeExtInfoSt> lst = stctx.VIEW_ST_FEEDETAILINFO.Where(n => (_iProjid == 0 ? true : n.projid == _iProjid)
                                                                                                                       && (n.rplb == _strRplb)
                                                                                                                       && (_iCusid == 0 ? true : n.cusid == _iCusid)
                                                                                                                       && (n.comcd == _strComcd)
                                                                                                                       && (string.IsNullOrEmpty(_strArrearslb) ? true : 
                                                                                                                                (_strArrearslb==GlobalSettings.NOLB?
                                                                                                                                    (_strRplb==GlobalSettings.FEELB_PAY?n.amt_pay!=n.amt_settled:n.amt_rec!=n.amt_settled)
                                                                                                                                   :(_strRplb==GlobalSettings.FEELB_PAY?n.amt_pay==n.amt_settled:n.amt_rec==n.amt_settled)))).AsEnumerable()
                                                                                                        .Select(n => new BizFeeExtInfoSt
                                                                                                        {
                                                                                                            amt_payprice = n.amt_payprice,
                                                                                                            amt_paytotal = n.amt_paytotal,
                                                                                                            amt_recprice = n.amt_recprice,
                                                                                                            amt_rectotal = n.amt_rectotal,
                                                                                                            billid = n.billid,
                                                                                                            billpid = n.billpid,
                                                                                                            billpidnm = n.billpidnm,
                                                                                                            brand = n.brand,
                                                                                                            brandnm = n.brandnm,
                                                                                                            comcd = n.comcd,
                                                                                                            createdt = n.createdt.ToString("yyyy-MM-dd"),
                                                                                                            creator = n.creator,
                                                                                                            ctid = n.ctid,
                                                                                                            ctsubid = n.ctsubid,
                                                                                                            cuscd = n.cuscd,
                                                                                                            cusid = n.cusid,
                                                                                                            cusnm = n.cusnm,
                                                                                                            feeid = n.feeid,
                                                                                                            invotp = n.invotp,
                                                                                                            invotpnm = n.invotpnm,
                                                                                                            model = n.model,
                                                                                                            num = n.num,
                                                                                                            objdesc = n.objdesc,
                                                                                                            objtp = n.objtp,
                                                                                                            objtpnm = n.objtpnm,
                                                                                                            optlb = GlobalSettings.ENTITYOPERATION_NONE,
                                                                                                            pid = n.pid,
                                                                                                            piddesc = n.piddesc,
                                                                                                            projdtlid = n.projdtlid,
                                                                                                            projid = n.projid,
                                                                                                            projdtlnm = n.dtlnm,
                                                                                                            amt_bill = (n.rplb == GlobalSettings.FEELB_PAY ? n.amt_pay : n.amt_rec),
                                                                                                            amt_settled = Math.Round(n.rplb == GlobalSettings.FEELB_PAY ? (n.amt_paytotal * (n.amt_pay == 0 ? 0 : (n.amt_settled / n.amt_pay))) : (n.amt_rectotal * (n.amt_rec == 0 ? 0 : (n.amt_settled / n.amt_rec))), 2),
                                                                                                            amt_settled_total = n.amt_settled,
                                                                                                            settlepercent = Math.Round(n.rplb == GlobalSettings.FEELB_PAY ? (n.amt_pay == 0 ? 1 : (n.amt_settled / n.amt_pay)) : (n.amt_rec == 0 ? 1 : (n.amt_settled / n.amt_rec)), 2),
                                                                                                            purchasdt = n.purchasdt.ToString("yyyy-MM-dd HH:mm"),
                                                                                                            remark = n.remark,
                                                                                                            spec = n.spec,
                                                                                                            taxrate = n.taxrate,
                                                                                                            rplb = n.rplb,
                                                                                                            unit = n.unit
                                                                                                        }).ToList();
                respReturn.result = new ResultStBizFee { feestlist = lst };
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (stctx.Connection.State != ConnectionState.Closed) stctx.Connection.Close(); }
            return respReturn;
        }


    }
}